package com.liu.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.domain.GeoMechanicsNodeVO;
import com.liu.domain.GeoMechanicsPO;
import com.liu.mapper.GeoMechanicsMapper;
import com.liu.service.GeoMechanicsService;
import org.springframework.beans.BeanUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.FileCopyUtils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LIU
 * @version 1.0.0
 * @date 2025/9/28
 */
@Service
public class GeoMechanicsServiceImpl extends ServiceImpl<GeoMechanicsMapper, GeoMechanicsPO> implements GeoMechanicsService {

    /**
     * 创建井
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createWell() {
        GeoMechanicsPO parent = this.baseMapper.selectOne(new QueryWrapper<GeoMechanicsPO>().eq("code","InterpretWellList"));

        GeoMechanicsPO po = new GeoMechanicsPO();
        po.setParentId(parent.getId());
        po.setAncestors(parent.getAncestors()+ ","+parent.getId());
        po.setLabel("地质力学解释");
        po.setClassName("GInterpretWell");
        po.setCode("Well");
        po.setCurveConfig("{}");
        this.baseMapper.saveGeoMechanics(po);

        List<GeoMechanicsNodeVO> nodes = getWellDefaultConfig();
        // 子节点保存
        saveChildren(nodes, po);
    }


    /**
     * 获取建井默认配置
     *
     * @return
     */
    public List<GeoMechanicsNodeVO> getWellDefaultConfig() {
        List<GeoMechanicsNodeVO> nodes = new ArrayList<>();
        try {
            ClassPathResource resource = new ClassPathResource("geoMechanics/GeoInterpretWell.json");
            String str = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8));
            nodes = JSON.parseArray(str, GeoMechanicsNodeVO.class);
        } catch (IOException e) {
            throw new RuntimeException("子节点保存异常", e);
        }
        return nodes;
    }

    /**
     * 保存子节点
     *
     * @param nodes  子节点集合
     * @param parent 父节点
     */
    private void saveChildren(List<GeoMechanicsNodeVO> nodes, GeoMechanicsPO parent) {
        for (GeoMechanicsNodeVO node : nodes) {
            node.setParentId(parent.getId());
            node.setAncestors(parent.getAncestors() + "," + parent.getId());

            GeoMechanicsPO po = new GeoMechanicsPO();
            BeanUtils.copyProperties(node, po);
            this.baseMapper.saveGeoMechanics(po);

            List<GeoMechanicsNodeVO> children = node.getChildren();
            if (children != null && !children.isEmpty()) {
                saveChildren(children, po);
            }
        }
    }
}
